clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

set more off
cd "$dataclean"


//loop through each location
*115/122
set more off 
set scheme s2color
use rounding_results_weekly_expand_kendall2, clear
summ group
local locationmax = r(max)
*156
 forvalues l =1(1)156{
 use multiplier_details_`l'.dta, clear
  di `l'
  if _N==1 continue
 
 capture noisily drop _seq _end _spell
 summ location 
 //determine the modal day each transition for each firm happens.  choose the modal day based on switches within inventory lots.
 

 
cd "$dta"
mmerge location using zipnew.dta, ukeep(taxcode zipnew zip) unmatched(master) type(n:1)
 
 gen quarteryr=qofd(date)
 mmerge quarteryr zipnew using "stateloctax2015Q1-2017Q4.dta", type(n:1) umatch(quarteryr zip) ukeep(stateloctax) unmatched(master)
 mmerge quarteryr zip using "stateloctax2015Q1-2017Q4.dta", type(n:1) umatch(quarteryr zip) ukeep(stateloctax) urename(stateloctax stateloctax_old) unmatched(master)
 replace stateloctax=stateloctax_old if stateloctax==.
cd "$dataclean"

 drop stateloctax_old

 drop if stateloctax==.
 
  if _N<=1 continue
  
  egen mindate=min(date), by(location)
  gen tmpx=stateloctax if mindate==date
  egen minstateloctax=min(tmpx), by(location)
  replace stateloctax=minstateloctax if (taxcode==11|taxcode==311|taxcode==911)&location!=240&location!=247&location!=449&location!=472&location!=687&location!=692 /*see if this edit helps out these firms*/
  drop tmpx
  *for firms that don't change their rate but follow a less neat rule (coming from fully flex stage 5 plots):
  replace stateloctax=.081 if location==240
  replace stateloctax=.077 if location==247
  replace stateloctax=.085 if location==330
 replace stateloctax=.084 if location==764
  replace stateloctax=.087 if location==793
 
  
  *and other firms that need a tax rate fix (coming from fully flex stage 5 plots):
  replace stateloctax=.088 if location==233
   replace stateloctax=.085 if location==330
   replace stateloctax=.085 if location==408
 replace stateloctax=.096 if location==437
 replace stateloctax=.089 if location==609
 
  
  gen taxmultnone=1
  gen taxmultsales=1+stateloctax
  gen taxmultboth=(1+stateloctax)*1.25 if date<20270
  replace taxmultboth=1+stateloctax +.37 if date>=20270
  gen taxmultbothdiv=taxmultboth 
  replace taxmultbothdiv=taxmultboth/(1+stateloctax) if date>=20270
  gen taxmultexcise=taxmultnone 
  replace taxmultexcise=1.37 if date>=20270
  

  foreach x in taxmultnone taxmultsales taxmultboth taxmultbothdiv taxmultexcise {
	qui gen price0 = price*`x'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	qui gen round = 1 if check<.01
	qui replace round = 0 if round==.
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*`x'	
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round

forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99			
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1


	qui egen max_round`x' = mean(round), by(inventoryid)
	rename round daily_round`x'
	drop round_price0 dist d_cut price1 d_cut_b4
	}
	
	//how round is each tax mult?  wider range of roundness for plots	
foreach x in taxmultnone taxmultsales taxmultboth taxmultbothdiv taxmultexcise {
	qui gen price0 = price_cleaned*`x'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	qui gen round = 1 if check<.02
	qui replace round = 0 if round==.
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price_cleaned*`x'
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*qui replace discount = round(discount, .01)
	
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.01&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.01&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.01&discount<.99			
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.01&discount<.99
		}
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1


	rename round daily_roundw`x'
	drop round_price0 dist d_cut price1 d_cut_b4 
	}
	
	
//start with the most common set of tax multipliers:
qui gen taxmultfinal=taxmultsales
qui replace taxmultfinal=taxmultboth if date>=20270
qui gen max_round_tax=max_roundtaxmultsales
qui replace max_round_tax=max_roundtaxmultboth if date>=20270

qui gen daily_round_tax=daily_roundwtaxmultsales
qui replace daily_round_tax=daily_roundwtaxmultboth if date>=20270

*now replace with other multipliers in a particular order if more round. first before tax change:
foreach x in taxmultnone {
qui replace taxmultfinal=0  if max_round`x'==max_round_tax&date<20270
qui replace taxmultfinal=`x'  if max_round`x'>max_round_tax&date<20270
qui replace daily_round_tax=daily_roundw`x'  if max_round`x'>max_round_tax&date<20270
qui replace max_round_tax=max_round`x'  if max_round`x'>max_round_tax&date<20270
}

foreach x in taxmultbothdiv taxmultexcise {
qui replace taxmultfinal=0  if max_round`x'==max_round_tax&date>=20270
qui replace taxmultfinal=`x'  if max_round`x'>max_round_tax&date>=20270
qui replace daily_round_tax=daily_roundw`x'  if max_round`x'>max_round_tax&date>=20270
qui replace max_round_tax=max_round`x'  if max_round`x'>max_round_tax&date>=20270
}


//make figures including i just want to see the stage 4 plot with final_mult_new & final_mult_new2. and with side by side roundedness (also kick in stage3 roundedness).
format taxmultfinal %09.3f
replace daily_round_tax=. if taxmultfinal==.
replace taxmultfinal=0 if taxmultfinal==.
qui tab taxmultfinal, gen(multfinal)
qui egen group_final=group(taxmultfinal)
qui summ group_final
local max=r(max)
	forvalues r = 1(1)`max' {
	qui egen sum_multfinal`r'= sum(multfinal`r'), by(weekly)
	qui replace sum_multfinal`r'=. if sum_multfinal`r'==0
		qui sum taxmultfinal if group_final==`r'
		local name = round(r(mean), .001)
		label var  sum_multfinal`r' "tax mult = `name'"
	}
	qui sum location
	local location = r(mean)
	
	qui egen round_weekly=mean(daily_round_tax), by(weekly)
	label var  round_weekly "Tax Round"
	

 
	/* plots
	sort weekly
	twoway (line sum_multfinal* weekly)  || (line round_weekly  weekly, yaxis(2)) , ///  
	xline(2886, lcolor(black) lpattern(dash))  title("Tax Mult, All Transactions: `location'") ///
	ytitle("Multiplier Count", margin(medsmall))  xtitle("Week") ylabel(0(.2)1, axis(2))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five))	
	qui graph export tax_mult_`location'.png, as(png) replace
	*/
 
	
	save multiplier_tax_`location'.dta, replace

 
 }
